package com.learning.jquizenglish.network.connection;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;

import com.google.gson.Gson;

public class JsonUtil {
    /**
     * convert a object from a json string
     * 
     * @param json
     * @param returnType
     * @return
     * @throws JsonPaserExceptions
     */
    public static <T> T fromString(String json, Class<T> returnType) throws JsonPaserExceptions {
//        T ret = null;
//        if (json != null) {
//            ObjectMapper mapper = new ObjectMapper();
//            mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//            try {
//                ret = mapper.readValue(json, returnType);
//            } catch (JsonParseException e) {
//                e.printStackTrace();
//                throw new JsonPaserExceptions(e);
//            } catch (JsonMappingException e) {
//                e.printStackTrace();
//                throw new JsonPaserExceptions(e);
//            } catch (IOException e) {
//                e.printStackTrace();
//                throw new JsonPaserExceptions(e);
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new JsonPaserExceptions(e);
//            }
//        }
    	T ret = null;
        if (json != null) {
        	Gson gson = new Gson();
        	ret = gson.fromJson(json, returnType);
        }
        return ret;
    }
    
    /**
     * convert a object from a json string
     * @param json
     * @param returnType
     * @return
     */
    public static <T> T convertObjectFromJsonString(String json, Class<T> returnType) {
//    	T ret = null;
//    	if (json != null) {
//        	ObjectMapper mapper = new ObjectMapper();
//        	mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        	try {
//    			ret = mapper.readValue(json, returnType);
//    		} catch (JsonParseException e) {
//    			e.printStackTrace();
//    			return null;
//    		} catch (JsonMappingException e) {
//    			e.printStackTrace();
//    			return null;
//    		} catch (IOException e) {
//    			e.printStackTrace();
//    			return null;
//    		} catch (Exception e) {
//    			e.printStackTrace();
//    			return null;
//    		}
//    	}
    	T ret = null;
        if (json != null) {
        	Gson gson = new Gson();
        	ret = gson.fromJson(json, returnType);
        }
    	return ret;
    }
    public static <T> ArrayList<T> convertArrayListFromJsonString(String json, Class<T> item_class) {
//    	if(json == null)
//    		return null;
//    	
//    	ArrayList<T> ret = null;
//    	ObjectMapper mapper = new ObjectMapper();
//    	
//    	mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//    	try {
//    		JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, item_class);
//			ret = mapper.readValue(json, type);
//		} catch (JsonParseException e) {
//			e.printStackTrace();
//			return null;
//		} catch (JsonMappingException e) {
//			e.printStackTrace();
//			return null;
//		} catch (IOException e) {
//			e.printStackTrace();
//			return null;
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
    	ArrayList<T> ret = new ArrayList<T>();
        if (json != null) {
        	Gson gson = new Gson();
        	List<Map> tmpList = gson.fromJson(json, List.class);
        	for (Map map : tmpList) {
				String tmpJson = gson.toJson(map);
				ret.add(gson.fromJson(tmpJson, item_class));
			}
        }
        return ret;
    }

//    public static <T> T fromFile(File file, Class<T> returnType) {
//        T ret = null;
//        if (file.exists()) {
//            ObjectMapper mapper = new ObjectMapper();
//            mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//            try {
//                ret = mapper.readValue(file, returnType);
//            } catch (JsonParseException e) {
//                e.printStackTrace();
//            } catch (JsonMappingException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return ret;
//    }
//
//    public static <T> ArrayList<T> fromStringToArray(String json, Class<T> item_class) {
//        if (json == null) return null;
//        ArrayList<T> ret = null;
//        ObjectMapper mapper = new ObjectMapper();
//        mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
//        try {
//            ret = mapper.readValue(json, new TypeReference<ArrayList<T>>() {});
//        } catch (JsonParseException e) {
//            e.printStackTrace();
//            return null;
//        } catch (JsonMappingException e) {
//            e.printStackTrace();
//            return null;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//        return ret;
//    }
//
//    public static String writeString(Object o) {
//        String ret = null;
//        if (o != null) {
//            ObjectMapper om = new ObjectMapper();
//            try {
//                ret = om.writeValueAsString(o);
//            } catch (JsonGenerationException e) {
//                e.printStackTrace();
//            } catch (JsonMappingException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return ret;
//    }

    /**
     * Get String from file json in asset folder.
     * 
     * @param context
     * @param name of file in asset such as "name.json" or "name.txt"
     * @return string json.
     * @throws IOException
     */
    public static String getStringFromAsset(Context context, String name) throws IOException {
        InputStream input;
        input = context.getAssets().open(name);
        int size = input.available();
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();

        // byte buffer into a string
        String jsonString = new String(buffer);
        return jsonString;
    }
}
